function [dat, trialsummary] = read_trace(filename)

fid = fopen(filename, 'r');

cline = fgetl(fid);

trialsummary = struct('experiment',[],'block',[],'trial',[],...
    'speed',[],'occ',[],'fixation',[],'stimulus',[]);

global condition

% while isempty(regexp(cline, 'MSG.*BLOCK', 'ONCE'))
%   cline = fgetl(fid);
% end
% temp = textscan(cline, '%s %u %s %f');
% trialsummary.block = temp{4};

blocktrial = sscanf(filename,'%*d %*c %d %*c %d %*s');
trialsummary.block = blocktrial(1,1);
trialsummary.trial = blocktrial(2,1);

% while isempty(regexp(cline, 'MSG.*TRIAL', 'ONCE'))
%   cline = fgetl(fid);
% end
% temp = textscan(cline, '%s %u %s %f');
% trialsummary.trial = temp{4};

while isempty(regexp(cline, 'MSG.*SUBJECT', 'ONCE'))
  cline = fgetl(fid);
end

temp = textscan(cline, '%s %u %s %f');
trialsummary.subject = temp{4};

if condition == 4
try  % new versions of the script don't have an experiment variable
    while isempty(regexp(cline, 'MSG.*EXPERIMENT', 'ONCE'))
        cline = fgetl(fid);
    end
    temp = textscan(cline, '%s %u %s %f');
    trialsummary.experiment = temp{4};
end
end

while isempty(regexp(cline, 'MSG.*FPC', 'ONCE'))
  cline = fgetl(fid);
end
temp = textscan(cline, '%s %u %s %f');
trialsummary.speed.fpc = temp{4};

while isempty(regexp(cline, 'MSG.*OCC_X', 'ONCE'))
  cline = fgetl(fid);
end
temp = textscan(cline, '%s %u %s %f');
trialsummary.occ.x = temp{4};

while isempty(regexp(cline, 'MSG.*OCC_W', 'ONCE'))
  cline = fgetl(fid);
end
temp = textscan(cline, '%s %u %s %f');
trialsummary.occ.w = temp{4};

while isempty(regexp(cline, 'MSG.*FIXATION', 'ONCE'))
  cline = fgetl(fid);
end
temp = textscan(cline, '%s %u %s %f');
trialsummary.fixation = temp{4};


while isempty(regexp(cline, 'MSG.*STIMULI', 'ONCE'))
    cline = fgetl(fid);
end
temp = textscan(cline, '%s %u %s %s');
trialsummary.stimulus.eq = temp{4};

if condition == 4
    
    trialsummary.stimulus.smooth = strcmp('cos(x*2*pi)',temp{4});
    trialsummary.stimulus.eq = temp{4};
else
    try
        noiseandseed = textscan(cline, '%s %u %s %*67s');                          % picks out string from noise,seed
        trialsummary.stimulus.smooth = strncmp('0.00',noiseandseed{1,1}(2,1),4);   % compares first 4 characters of noiseandseed (i.e.just the noise) to 0.00
    catch
        trialsummary.stimulus.smooth = strcmp('cos(x*2*pi)',temp{4});
        trialsummary.stimulus.eq = temp{4};
    end
        
end

while isempty(regexp(fgetl(fid), 'MSG.*START', 'ONCE')); end;

dat = [];
cline = fgetl(fid);
sac = 0;
while isempty(regexp(cline, 'MSG.*END', 'ONCE'))
    cline = fgetl(fid);
    if ~isempty(regexp(cline, 'SSACC','ONCE'))
        sac = 1;
    elseif ~isempty(regexp(cline, 'SFIX','ONCE'))
        sac = 0;
%     elseif ~isempty(regexp(cline, 'SBLINK','ONCE'))
%         dat = [dat; NaN];
    elseif ~isempty(str2num(strrep(cline, '...', '')))
        dat = [dat; str2num(strrep(cline, '...', '')), sac];
    end
    %   if strncmp('EFIX', cline, 4)
    %
    %   end
    %   dat = [dat; str2num(strrep(cline, '...', ''))];
    %   cline = fgetl(fid);
end


fclose(fid);
%%